
<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%><%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@page import="com.spmanage.util.WebAction"%>
<%@page import="com.spmanage.util.StringUtil"%><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%
	WebAction webAction = new WebAction(request,response);
    System.out.println("空中接口-上行同步");
    String userPhone = webAction.getParameterString("userNumber");
    String spNo = webAction.getParameterString("spNumber");//端口号
    String linkId = webAction.getParameterString("linkId");
    String fee = webAction.getParameterString("fee");
    String mo = webAction.getParameterString("msg");//上行
    String date = webAction.getParameterString("getDate");
    String province = webAction.getParameterString("province");
    String innerid = webAction.getParameterString("innerid");
    String provincename = webAction.getParameterString("provincename");
    String channel = webAction.getParameterString("channel");
    String gatewayId = webAction.getParameterString("gatewayId");
    String srvCode = webAction.getParameterString("srvCode");
    String feeType = webAction.getParameterString("feeType");
    
    //指令处理,返回处理后的指令
	String strMo = BillingUtil.getMo(spNo, mo);
	System.out.println("空中接口-上行同步：原始上行:"+mo+";处理后的上行："+strMo);
    //获取用户渠道号
    int channelId = BillingUtil.getUserChannel(spNo,strMo);
    //sp标识，类似于网关号
    int spid = BillingUtil.getUserSpId(spNo,strMo);
    //省份
    int provinceId = BillingUtil.getProvinceId(userPhone);
    
	//---------------扣量处理
	//得到CP渠道扣量比例数
    int cpKouLiangShu = BillingUtil.getCpKouLiangBiLv(channelId);
	int cpKouLiang = 0;
	int ischeck = 0;
    //得到CP渠道指令扣量比例数
    int cpMoKouLiangShu = BillingUtil.getCpMoKouLiangBiLv(channelId,spid,strMo);
    if(cpMoKouLiangShu!=0){
    	cpKouLiang = cpMoKouLiangShu;
    	int cpauoid = BillingUtil.getCpAutoId(channelId, spid, strMo);//查询cp对应的autoid，和下面进行区分，以免计数不对
		ischeck = BillingUtil.getCheckRecv(cpauoid,cpKouLiang);//获取是否要扣量
    }else{
    	cpKouLiang = cpKouLiangShu;
		ischeck = BillingUtil.getCheckRecv(channelId,cpKouLiang);//获取是否要扣量，这里直接用渠道号
    }
    //---------------
    
    StringBuilder sb = new StringBuilder();
    sb.append("'");
    sb.append(spid);
    sb.append("',");
    sb.append("'");
    sb.append(userPhone);
    sb.append("',");
    sb.append("'");
    sb.append(spNo);
    sb.append("',");
    sb.append("");
    sb.append("getDate()");
    sb.append(",");
    sb.append("'");
    sb.append("msgid");
    sb.append("',");
    sb.append("'");
    sb.append("mid");
    sb.append("',");
    sb.append("'");
    sb.append("1");//cardtype
    sb.append("',");
    sb.append("'");
    sb.append(channelId);//channel
    sb.append("',");
    sb.append("'");
    sb.append("101");//prdid
    sb.append("',");
    sb.append("'");
    sb.append("010");//telid
    sb.append("',");
    sb.append("'");
    sb.append(linkId);//linkid
    sb.append("',");
    sb.append("'");
    sb.append(mo);//上行内容
    sb.append("'");
    sb.append(",");
    sb.append(provinceId);
    sb.append(",");
    sb.append(ischeck);
    SqlUtil.executeUpdate("insert into "+BillingUtil.getRecvDay()+"(areaid,org,dest,tm,msgid,mid,cardtype,channel,prdid,teleid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
    date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());//200912121200
    System.out.println("空中接口-上行同步是否扣量："+ischeck+",cp:"+channelId+",spNo:"+spNo+",mo:"+mo+",渠道扣量比例："+cpKouLiangShu+",指令扣量比例："+cpMoKouLiangShu+",最终比例扣量："+cpKouLiang);
    if(ischeck == 0) {
    	System.out.println("空中-上行同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5));
    	String http = BillingUtil.getMoUrlByChannel(channelId);
    	if(http!=null&&http.length()>0) {
    		StringBuilder moUrl = new StringBuilder(http);
    		if(moUrl.indexOf("?")>0){
    			moUrl.append("&userPhone=");
    		}else{
    			moUrl.append("?userPhone=");
    		}
        	moUrl.append(userPhone);
        	moUrl.append("&spNo=");
        	moUrl.append(spNo);
        	moUrl.append("&linkId=");
        	moUrl.append(linkId);
        	moUrl.append("&mo=");
        	moUrl.append(URLEncoder.encode(mo,"UTF-8"));
        	moUrl.append("&date=");
        	moUrl.append(date);
    	    System.out.println("空中-上行同步url:"+moUrl.toString());
    	    String str=HttpUtil.getHtmlSource(moUrl.toString(),"UTF-8");
    	    System.out.println("空中-同步出去的地址:"+str);
    	}
    }
    
    response.getWriter().write("OK");
    //String msg = StringUtil.decodeStringByUTF8(webAction.getParameterString("msg"));
	
	//System.out.println("msg:"+msg);
	//合作方url
	/*
	String url="";
	mo = mo.toUpperCase();
	if(ischeck==0&&mo.substring(0,5).equals("8DUU1")&&spNo.equals("10663355")){		
		url="http://60.191.223.25:8690/891/mo.aspx";
		System.out.println("空中-迈缘网络-上行同步");
		//url="http://jk.dsmnb.com:8690/891/mo.aspx";
		url=url+"?userPhone="+userPhone+"&spNo="+spNo+"&linkId="+linkId+"&mo="+content+"&date="+URLEncoder.encode(date,"UTF-8")+"";
		System.out.println(url);
		HttpUtil.getHtmlSource(url,"UTF-8");
		//BillingUtil.sendUrl(url);
	}
	else if(ischeck==0&&mo.substring(0,6).equals("8DYU20")&&(spNo.equals("10663355")||spNo.equals("10661660"))){
		url="http://www.3gmeeting.net/php/sp/lj.php";
		System.out.println("空中-哗宇通-上行同步");
		url=url+"?userPhone="+userPhone+"&spNo="+spNo+"&linkId="+linkId+"&mo="+content+"&date="+URLEncoder.encode(date,"UTF-8")+"";
		System.out.println(url);
		HttpUtil.getHtmlSource(url,"UTF-8");
	}else if(ischeck==0&&mo.substring(0,6).equals("8DUU20")&&(spNo.equals("10663355")||spNo.equals("10661660"))){
		url="http://www.3gmeeting.net/php/sp/lj.php";
		System.out.println("空中-哗宇通-上行同步"+mo);
		//url="http://jk.dsmnb.com:8690/891/mo.aspx";
		url=url+"?userPhone="+userPhone+"&spNo="+spNo+"&linkId="+linkId+"&mo="+content+"&date="+date+"";
		System.out.println(url);
		HttpUtil.getHtmlSource(url,"UTF-8");
	}
	else if(ischeck==0&&mo.substring(0,6).equals("8DYU21")&&(spNo.equals("10663355")||spNo.equals("10661660"))){
		url="http://119.145.9.39:8888/sms/land_kongjian_mo_1021.jsp";
		System.out.println("空中-五洲无线-上行同步");
		url=url+"?userPhone="+userPhone+"&spNo="+spNo+"&linkId="+linkId+"&mo="+content+"&date="+URLEncoder.encode(date,"UTF-8")+"";
		System.out.println(url);
		HttpUtil.getHtmlSource(url,"UTF-8");
	}*/
%>
